import axios from 'axios'
//1.实例化axios对象
var service=axios.create({
    timeout:10*1000,
    baseURL: '', 
})
//2.设置请求拦截器
service.interceptors.request.use(
    (config)=>{
        // console.log('service.interceptors.request');
        //设置请求头参数( 携带请求头参数 )
        config.headers['Authorization'] = 'Bearer' + ' ' + localStorage.getItem('token');
        return config;
    },
    (error)=>{
        return Promise.reject(error);
    }
)
//2.设置响应拦截器
service.interceptors.response.use(
    (res)=>{
        return res;
    },
    (error)=>{
        if( error.response.status == 401 ){
            alert('登陆过期,请重新登录!');
            //自动删除过期的token
            //自动跳转到登录页
        }
        else if( error.response.status == 404 ){
            alert('访问路径有误!')
        }
        else if( error.response.status == 500 ){
            alert('服务器内部错误!')
        }
        return Promise.reject(error);
    }
)

//3.导出service
export default service;